CUBE CONNECT Edition Help

Update links or nodes attributes with the CubeDatabase class

The usage of Lua Expressions to update network attributes is explain in Usage of Lua Expressions within the CubeDatabase class. To update links or nodes attributes, without using Lua expressions, is possible by using available methods.

Firstly, to create a new attribute, for example for links, the addLinkAttribute can be used as shown in the example below:
# Creating new attribute called fftime, storing free-flow travel time based on the speed attribute
fftime_attr = db.addLinkAttribute(network_name, "fftime", cp.FieldType_Real)

Where the arguments are the network name, the new attribute name and the FieldType code (we would recommend not using numbers for the enumerated types as these numbers may change).

To update the new or existing links (or nodes) attributes is possible by defining a CubeNetworkEntityBuffer (as explained in “Looping through the links or nodes with the CubeDatabase class”) and using setInteger, setInteger64, setReal, setString or setStringByValue methods while looping through the links, as in the example below:

for link_ix in range(links_buffer.numEntities()):
    distance_value = links_buffer.real(link_ix, "distance")
    speed_value = links_buffer.real(link_ix, "speed")
    fftime_value = 60 * (distance_value / speed_value)
    links_buffer.setReal(link_ix, "fftime", fftime_value)

The syntax is similar for updating node attributes, as in the example below:

for node_ix in range(nodes_buffer.numEntities()):
    node_n = nodes_buffer.n(node_ix)
    farezone_value = int(nodes_buffer.real(node_ix, "farezone"))
    mode_value = 0
    if farezone_value > 0:
        mode_value = 1
        nodes_buffer.setInteger(node_ix, "mode", mode_value)

After the loop, the changes are stored in memory for the buffer, it is therefore necessary to update the database using the updateNetworkFromBuffer method as below:

db.updateNetworkFromBuffer(network_name, nodes_buffer)